package cn.xenosp.server.web.controller;

import cn.hutool.core.util.IdUtil;
import cn.xenosp.server.common.Result;
import cn.xenosp.server.common.annotation.LogInfo;
import cn.xenosp.server.common.tools.FileUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 项目名： service-web
 * 包路径： cn.xenosp.server.web.controller
 * 作者：   TongHui
 * 创建时间: 2024-12-18 16:44
 * 描述: TODO
 * 版本: 1.0
 */
@RestController
@RequestMapping("${app.config.api-base-url}/file/")
public class FileController {


    @LogInfo(operationModuleType = "文件模块",operationName = "文件上传",operationContent = "上传文件数据")
    @PostMapping("/upload")
    public Result<?> upload(@RequestParam("file") MultipartFile multipartFile){
        try {
            String path = System.getProperty("user.dir") + "\\file";
            String fileId = IdUtil.simpleUUID();
            String filename = multipartFile.getOriginalFilename();
            String suffix = filename.substring(filename.lastIndexOf(".") + 1);

            File file = new File(path+"\\"+fileId+"."+suffix);
            File parentFile = file.getParentFile();
            if (!parentFile.exists()){
                parentFile.mkdirs();
            }
            try (FileOutputStream out = new FileOutputStream(path+"\\"+fileId+"."+suffix)) {
                out.write(multipartFile.getBytes());
                out.flush();
            }
            return Result.OK("上传成功",fileId+"."+suffix);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.error("上传失败",null);
    }
    @LogInfo(operationModuleType = "文件模块",operationName = "文件批量上传",operationContent = "批量上传文件数据")
    @PostMapping("/uploads")
    public Result<?> uploads(@RequestParam("files") MultipartFile[] multipartFiles){
        try {
            for (MultipartFile multipartFile : multipartFiles) {
                String path = System.getProperty("user.dir") + "\\file";
                String fileId = IdUtil.simpleUUID();
                String filename = multipartFile.getOriginalFilename();
                String suffix = filename.substring(filename.lastIndexOf(".") + 1);

                File file = new File(path+"\\"+fileId+"."+suffix);
                File parentFile = file.getParentFile();
                if (!parentFile.exists()){
                    parentFile.mkdirs();
                }
                try (FileOutputStream out = new FileOutputStream(path+"\\"+fileId+"."+suffix)) {
                    out.write(multipartFile.getBytes());
                    out.flush();
                }
                return Result.OK("上传成功",fileId);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.error("上传失败",null);
    }
    @LogInfo(operationModuleType = "文件模块",operationName = "文件下载",operationContent = "文件下载")
    @PostMapping("/download/{id}")
    public void download(HttpServletResponse response, @PathVariable("id") String id){
        String path = System.getProperty("user.dir") + "\\file";
        FileUtil.dowInline(FileUtil.readBytes(new File(path+"\\"+id)),id,response);
    }
}
